package com.lele.listeners;

import com.lele.ioc.BeanFactory;
import com.lele.ioc.BeanFactoryImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

// 上下文监听器
@WebListener // 监听器注解
public class ContextLoaderListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ContextLoader--我出生啦！");
        // 在上下文作用域创建的时候就把 beanFactory 放入 上下文作用域
        BeanFactory beanFactory = new BeanFactoryImpl();
        ServletContext application = sce.getServletContext();
        application.setAttribute("beanFactory",beanFactory);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ContextLoader--我销毁啦！");
    }
}
